import {useAdminStore} from '@/dt-lib/plug/pinia/stores/admin'

import {getStaticBaseUrl} from "./env.ts";
import {DepartmentTreeFilter} from "@/dt-lib/help/deptTree.ts";

import dayjs from 'dayjs';

const adminStore = useAdminStore()
/**
 * 时间戳 格式化
 */
export const formatDate = (timeStamp: number, yearFull: boolean = true, empty:string="") => {

    if (timeStamp > 0) {

        let date = new Date(timeStamp * 1000)
        let year = date.getFullYear()
        let month = date.getMonth() + 1
        let day = date.getDate();
        let h = date.getHours()
        let m = date.getMinutes()

        return (yearFull ? year : year.toString().slice(-2)) + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day) + ' ' + (h < 10 ? '0' + h : h) + ':' + (m < 10 ? '0' + m : m)

    }

    return empty
}

/**
 * 时间戳 格式化
 */
export const formatDateYmd = (timeStamp: number, yearFull: boolean = true, empty:string="") => {

    if (timeStamp > 0) {

        let date = new Date(timeStamp * 1000)
        let year = date.getFullYear()
        let month = date.getMonth() + 1
        let day = date.getDate()

        return (yearFull ? year : year.toString().slice(-2)) + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day)

    }

    return empty
}


/**
 * 获取当前 日期
 */
export const getDateYmd = (): string => {

    let datetime = new Date();
    let year = datetime.getFullYear();
    let month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    let date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();

    return year + '-' + month + '-' + date
}

/**
 * 客户类型 格式化
 */
export const formatCusTypeText = (type: number) => {

    if (type == 1) {
        return '新客'
    }

    return '老客'

}

/**
 * 客户类型 css 格式化
 */
export const formatCusTypeCss = (status: number) => {

    if (status == 1) {
        return 'danger'
    }

    return 'info'

}

/**
 * 预约状态 格式化
 */
export const formatCartStatusText = (type: number) => {

    switch (type) {
        case 1:
            return '预约中'
        case 2:
            return '已开单'
        case 3:
            return '已确认'
        case 4:
            return '已取消'
        case 5:
            return '已超时'
        case 6:
            return '流失中'
        case 7:
            return '已流失'
        case 99:
            return '已删除'
        default:
            return '未知'
    }

}

/**
 * 预约状态 样式
 */
export const formatCartStatusCss = (type: number) => {

    switch (type) {
        case 1:
            return ''
        case 2:
            return 'success'
        case 3:
            return ''
        case 4:
            return ''
        case 5:
            return ''
        case 6:
            return ''
        case 7:
            return 'danger'
        case 99:
            return 'warning'
        default:
            return ''
    }

}


/**
 * 线索跟进状态 格式化
 */
export const formatClueFollow = (status: number) => {

    if (status == 2) {
        return '待跟进'
    }

    return '跟进中'

}

/**
 * 线索跟进状态 css 格式化
 */
export const formatClueFollowText = (status: number) => {

    if (status == 2) {
        return 'info'
    }

    return 'primary'

}

/**
 * 状态 格式化
 */
export const formatStatusText = (status: number) => {

    if (status == 1) {
        return '正常'
    }

    return '失效'

}

/**
 * 状态 css 格式化
 */
export const formatStatusCss = (status: number) => {

    if (status == 2) {
        return 'warning'
    }

    return 'success'

}

/**formatStatusCss
 * 状态 css 格式化
 */
export const formatStatus2Css = (status: number) => {

    if (status == 2) {
        return 'danger'
    }

    return 'success'

}

/**
 * 微信号类型 格式化
 */
export const wechatTypeText = (status: number) => {

    if (status == 1) {
        return '企微'
    }

    return '个微'

}

/**
 * 微信号类型 css 格式化
 */
export const wechatTypeCss = (status: number) => {

    if (status == 2) {
        return 'info'
    }

    return 'success'

}


export const isAdmin = (): boolean => {
    return adminStore.getAdmin.auth_group_desc === '管理员'
}

export const isValidPhone = (phone: string): boolean => {
    if (phone === '') {
        return false
    }
    const regex = /^1\d{10}$/;
    return regex.test(phone);
};

export const rSurelyPowered = () => {
    return

    let c1 = document.getElementsByClassName('surely-table-body') || []
    let c2 = document.getElementsByClassName('surely-table-body-viewport-container') || []
    let c3 = document.getElementsByClassName('surely-table-horizontal-scroll') || []

    for (let i = 0; i < c1.length; i++) {
        let _ele = c1[i].nextElementSibling
        if (_ele && _ele.innerHTML == 'Unlicensed Product') {
            _ele.innerHTML = ''
        }
    }

    for (let i = 0; i < c2.length; i++) {
        let _ele = c2[i].nextElementSibling
        if (_ele && _ele.innerHTML == 'Powered by Surely Vue') {
            _ele.innerHTML = ''
        }
    }

    for (let i = 0; i < c3.length; i++) {
        let _ele = c3[i].nextElementSibling
        if (_ele && _ele.innerHTML == 'Unlicensed Product') {
            _ele.innerHTML = ''
        }
    }
}


export const searchChinese = (sentences: string, searchTerm: string) => {
    const regex = new RegExp(searchTerm, 'u'); // 'u' 标志用于支持Unicode
    return regex.test(sentences);
}

export const filterLabel = (inputValue: string, path: []) => {
    return path.some(option => searchChinese(option.label, inputValue));
}

export const conversionTypeText = (type: string) => {

    switch (type) {
        case 'chat':
            return '聊天'
        case 'qa':
            return '问答'
        case 'qaStep':
            return '问答(步骤)'
        default:
            return '未定义'

    }


}

export const getFileUrl = (url: string): string => {

    if (url != '') {

        if (url.startsWith('http')) {
            return url
        }

        return getStaticBaseUrl() + url

    }

    return ''


}


export const fixedToArray = (x: number, y: number): [number, number] => {

    return [x, y]


}

export const bizFromStore = (): Array<Object> => {
    return adminStore.getBiz
}

export const bizShopCityFromStore = (biz_id: number | string): [] => {
    return adminStore.getBizShopCity.hasOwnProperty(biz_id) ? adminStore.getBizShopCity[biz_id] : []
}

export const bizShopFromStore = (biz_id: number | string): [] => {
    return adminStore.getBizShop.hasOwnProperty(biz_id) ? adminStore.getBizShop[biz_id] : []
}

export const bizBrandFromStore = (biz_id: number | string): [] => {
    return adminStore.getBizBrand.hasOwnProperty(biz_id) ? adminStore.getBizBrand[biz_id] : []
}

export const bizDeptFromStore = (biz_id: number | string, post: string = '') => {
    if (adminStore.getBizDept.hasOwnProperty(biz_id)) {

        const storeBizDept = adminStore.getBizDept[biz_id];
        const postArr = post.split(',')

        const filter = new DepartmentTreeFilter(storeBizDept)
        const tree = filter.filterByPost(postArr)
        const treeIds = filter.getFilteredDataIds('id')

        // console.log(filter.getFilteredDataIds('id'))

        return {
            tree: tree,
            ids: treeIds
        }

    }

}

export const bizDeptEmployeeFromStore = (biz_id: number | string, dept_id_arr: number[]): [] => {

    let deptEmployeeArr = []
    // console.log(biz_id)
    if (adminStore.getBizDeptEmployee.hasOwnProperty(biz_id)) {
        const storeBizBizDeptEmployee = adminStore.getBizDeptEmployee[biz_id]
        // console.log(storeBizBizDeptEmployee)
        if (dept_id_arr.length == 0) {
            for (let dept_id of Object.keys(storeBizBizDeptEmployee)) {
                // console.log(dept_id)
                deptEmployeeArr = [...deptEmployeeArr, ...storeBizBizDeptEmployee[dept_id]]
            }
        } else {
            for (let dept_id of dept_id_arr) {
                if (storeBizBizDeptEmployee.hasOwnProperty(dept_id)) {
                    deptEmployeeArr = [...deptEmployeeArr, ...storeBizBizDeptEmployee[dept_id]]
                }
            }
        }


    }

    return deptEmployeeArr

}


export const todayYMD = (): string => {
    return dayjs().format('YYYY-MM-DD')
}

